package com.xiaoxiong.text;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.util.Log;

import com.xiaoxiong.bean.CourseData;
import com.xiaoxiong.database.OperateFormCompulsoryCourse;

public class DealRequiredCourseInfo {
	private Context context;
	private String html;
	private Document content;
	private String[] Year;
	private String[] data;
	private List<CourseData> requiredCourseDatas = new ArrayList<CourseData>();
	private String[] YearFlag = new String[] { "第一学期", "第二学期", "第三学期", "第四学期",
			"第五学期", "第六学期", "第七学期", "第八学期" };

	public DealRequiredCourseInfo(Context context, String html) {
		super();
		this.context = context;
		this.html = html;
		this.content = Jsoup.parse(this.html);
		Year = new String[8];
		data = new String[9];
		for (int i = 0; i < 9; ++i) {
			data[i] = new String();
		}
	}

	public void StartDeal() {
		init();
		int end = 0;
		int f = 0;
		boolean flag = true;
		for (int i = 0; i < 8; ++i) {
			StringBuilder sbyear = new StringBuilder(Year[i]);
			while (sbyear.length() > 0) {
				f = 0;
				for (int m = 0; m < 9;) {
					flag = true;
					data[m] = "";
					end = sbyear.indexOf("@");
					for (int j = 0; j < end; ++j) {
						char c = sbyear.charAt(j);
						if (c == '无' && m == 0) {
							f++;
							flag = false;
							break;
						} else {
							data[m] += c;
						}
					}
					if (flag || f > 1) {
						++m;
					}
					sbyear.delete(0, end + 1);
				}
				if (f <= 1) {
					CourseData requireData = new CourseData(YearFlag[i],
							data[0], data[1], data[2], data[3], data[4],
							data[5], data[6], (data[6] + "------" + data[7]),
							data[8]);
					requiredCourseDatas.add(requireData);
				} else {
					CourseData requireData = requiredCourseDatas
							.get(requiredCourseDatas.size() - 1);
					requiredCourseDatas.remove(requiredCourseDatas.size() - 1);

					String score = requireData.getScore();

					requireData.setScore(score + "\n" + data[6] + "------"
							+ data[7]);

					requiredCourseDatas.add(requireData);
				}
			}
		}
		saveData();
	}

	private void saveData() {
		OperateFormCompulsoryCourse ofc = new OperateFormCompulsoryCourse(
				context);
		ofc.deleteAllCompulsoryCourse();
		int size = requiredCourseDatas.size();
		for (int i = 0; i < size; ++i) {
			CourseData requireData = requiredCourseDatas.get(i);
			ofc.insertCompulsoryCourse(requireData.getSemester(),
					requireData.getCourseCode(), requireData.getCourseName(),
					requireData.getCredit(), requireData.getExamMethod(),
					requireData.getFirstCourse(),
					requireData.getRepairCourse(), requireData.getStudyTime(),
					requireData.getScore(), requireData.getHadCredit());
		}
	}

	private void init() {
		Elements element = content.getElementsByTag("td");
		StringBuilder value = new StringBuilder();
		for (Element links : element) {
			String title = links.text();
			value.append(title + "\n");
		}
		
		value.delete(0, value.indexOf("第一学期"));
		value.delete(value.indexOf("必修课的总学分"), value.length());
		String str = value.toString().replaceAll("\n", "@");

		// 第一学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第一学期"));
		value.delete(value.indexOf("第二学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[0] = deal(value);
		// 第二学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第二学期"));
		value.delete(value.indexOf("第三学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[1] = deal(value);
		// 第三学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第三学期"));
		value.delete(value.indexOf("第四学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[2] = deal(value);
		// 第四学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第四学期"));
		value.delete(value.indexOf("第五学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[3] = deal(value);
		// 第五学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第五学期"));
		value.delete(value.indexOf("第六学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[4] = deal(value);
		// 第六学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第六学期"));
		value.delete(value.indexOf("第七学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[5] = deal(value);
		// 第七学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第七学期"));
		value.delete(value.indexOf("第八学期"), value.length());
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[6] = deal(value);
		// 第八学期的
		value = new StringBuilder(str);
		value.delete(0, value.indexOf("第八学期"));
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year[7] = deal(value);
		
//		for(int i = 0;i < 8;++i){
//			Log.d("year" + i, "第" + (i + 1) + "学期:\n" + Year[i] + "\n");
//		}

	}

	private String deal(StringBuilder sbValue) {
		StringBuilder sb = new StringBuilder();
		for (int j = 0; j < sbValue.length(); ++j) {
			char c = sbValue.charAt(j);
			sb.append(c);
			if (c == '@' && sbValue.charAt(j + 1) == '@') {
				sb.append("无");
			}
		}
		return sb.toString().replaceAll(" ", "");
	}
}
